<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :where="defaultWhere" v-if="search" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        :where="defaultWhere"
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>

        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>

        <template #images="{ row }">
          <span class="pointer blue" @click="openImages(row)"> 查看 </span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button
              type="info"
              size="small"
              @click="openEdit(row)"
              v-if="props.action == 'list'"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="del(row)"
              v-if="props.action == 'list'"
            >
              删除
            </el-button>
            <el-button
              type="success"
              size="small"
              @click="setStatus(row, 200)"
              v-if="props.action == 'judge'"
            >
              通过
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="setStatus(row, 500)"
              v-if="props.action == 'judge'"
            >
              拒绝
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit
      v-model="showEdit"
      :data="current"
      :company-id="props.companyId"
      @done="reload"
    />
    <images v-model="showImages" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './search.vue';
  import Edit from './edit.vue';
  import images from './images.vue';
  import {
    pageCompanyPaid,
    delCompanyPaid,
    setCompanyPaidStatus
  } from '@/api/company';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import { Plus } from '@element-plus/icons-vue';

  const props = defineProps({
    search: {
      type: Boolean,
      default: true
    },
    action: {
      type: String,
      default: 'list'
    },
    companyId: {
      type: Number,
      default: 0
    },
    siteId: {
      type: Number,
      default: 0
    },
    tenantId: {
      type: Number,
      default: 0
    },
    belongsDate: {
      type: String,
      default: ''
    },
    actionStatus: Number
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 80,
        align: 'center'
      },
      {
        slot: 'status',
        label: '状态',
        width: 90,
        align: 'center'
      },
      {
        prop: 'belongsDate',
        label: '所属月份',
        width: 105,
        align: 'center'
      },
      {
        prop: 'company',
        label: '企业',
        width: 220
      },
      {
        prop: 'amount',
        label: '金额',
        width: 100
      },
      {
        prop: 'remark',
        label: '备注'
      },
      {
        slot: 'images',
        label: '相关凭证',
        width: 90,
        align: 'center'
      },
      {
        prop: 'create_user',
        label: '提交',
        width: 80
      },
      {
        prop: 'create_time',
        label: '提交时间',
        width: 140
      }
    );

    if (props.action == 'list') {
      cols.push({
        columnKey: 'action',
        label: '操作',
        width: 130,
        slot: 'action',
        align: 'center',
        fixed: 'right'
      });
    }

    return cols;
  });

  const defaultWhere = {
    tenant_id: props.tenantId,
    site_id: props.siteId,
    status: props.actionStatus,
    company_id: props.companyId,
    belongs_date: props.belongsDate
  };

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageCompanyPaid({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  const showImages = ref(false);
  const openImages = (row) => {
    showImages.value = true;
    current.value = row;
  };

  const status = ref(props.actionStatus);
  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    status.value = where ? where.status : 0;
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 删除 */
  const del = (row) => {
    ElMessageBox.confirm(
      '确定要删除“' +
        row.company +
        row.position +
        row.amount +
        '”的开支记录吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        delCompanyPaid({ id: row.id })
          .then((msg) => {
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 通过 */
  const setStatus = (row, status) => {
    let action = status == 200 ? '通过' : '拒绝';
    ElMessageBox.confirm(
      action + row.company + row.amount + '的回款?',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading({
          message: '正在处理中..',
          mask: true
        });
        setCompanyPaidStatus({
          id: row.id,
          status: status
        })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功!');
            reload();
          })
          .catch(() => {
            loading.close();
          });
      })
      .catch(() => {});
  };
</script>
